package com.ews.onlineexchange.rest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.ews.onlineexchange.app.entity.BulletinStat;
import com.ews.onlineexchange.controller.QualificationClassHome;
import com.ews.onlineexchange.controller.QualificationClassList;
import com.ews.onlineexchange.controller.SectorHome;
import com.ews.onlineexchange.model.QualificationClass;
import com.ews.onlineexchange.model.QualificationLevel;
import com.ews.onlineexchange.model.Sector;
import com.ews.onlineexchange.service.QualificationClassService;
import com.ews.onlineexchange.util.DateUtil;


@Path(value = "/qualificationClass")
public class QualificationClassRest {
	@Inject
	EntityManager entityManager;

	@Inject
	QualificationClassService qualificationClassService;
	@GET
	@Path("/getQualificationClassStat")
	@Produces("application/json")
	@Context
	public List<BulletinStat> getQualificationClassStat(@QueryParam("startDate") String startDate,@QueryParam("endDate") String endDate)  {
		
			initDateParam(startDate, endDate);
			List<BulletinStat> qualificationStat = qualificationClassService.QualificationStat(sDate, eDate);
			return qualificationStat;
	}
	private Date sDate;
	private Date eDate;
	public void initDateParam(String startingDate,String endingDate) {
		if(org.apache.commons.lang3.StringUtils.isNotBlank(startingDate)) {
			try {
				sDate = DateUtil.getDate(startingDate);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		if(org.apache.commons.lang3.StringUtils.isNotBlank(endingDate)) {
			try {
				Date tempDate = DateUtil.getDate(endingDate);
				if(tempDate != null) {
					Calendar cal = Calendar.getInstance();
					cal.setTime(tempDate);
					cal.set(Calendar.HOUR_OF_DAY, 23);
					cal.set(Calendar.MINUTE, 59);
					cal.set(Calendar.SECOND, 59);
					this.eDate = cal.getTime();
				}
				sDate = DateUtil.getDate(startingDate);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	
	@Inject
	QualificationClassList qualificationClassList;
	
	@GET
	@Path("/getQualificationClassList")
	@Produces("application/json")
	@Context
	public List<QualificationClass> getQualificationClassList(){
		qualificationClassList.refresh();
		return qualificationClassList.getResultList();
	}
	
	@GET
	@Path("/getQualificationLvelByClass")
	@Produces("application/json")
	@Context
	public List<QualificationLevel> getQualificationLvelByClass(@QueryParam("classId") String classId){
		QualificationClass clazz = entityManager.find(QualificationClass.class, classId);
		return clazz.getSortedLevelOptions();
	}
	@Inject
	QualificationClassHome qualificationClassHome;
	
	@GET
	@Path("/getRootSectorsByClass")
	@Produces(MediaType.APPLICATION_JSON)
	@Context
	public Set<Sector> getRootSectorsByClass(@QueryParam("classId") String classId){
		qualificationClassHome.setId(classId);
		QualificationClass clazz = qualificationClassHome.getInstance();
//		clazz.getRootSectors().size();
		return clazz.getRootSectors();
	}
	@Inject
	SectorHome sectorHome;
	@GET
	@Path("/getChildSectorsByParent")
	@Produces(MediaType.APPLICATION_JSON)
	@Context
	public List<Sector> getChildSectorsByParent(@QueryParam("sectorId") String sectorId){
		sectorHome.setId(sectorId);
		Sector sector = sectorHome.getInstance();
//		clazz.getRootSectors().size();
		return sector.getChildSectors();
	}
	
	public Date getsDate() {
		return sDate;
	}

	public void setsDate(Date sDate) {
		this.sDate = sDate;
	}

	public Date geteDate() {
		return eDate;
	}

	public void seteDate(Date eDate) {
		this.eDate = eDate;
	}
	
}
